package com.ruoyi.quartz.race.init;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.quartz.race.model.RaceEntity;
import com.ruoyi.quartz.race.type.ServiceJc;
import com.ruoyi.quartz.util.JedisUtil;

import us.codecraft.webmagic.Spider;

public class ServiceHistory {
	public static void mainx(String[] args) {
		Set<String> keys = JedisUtil.keysByPre("raceList:");
		List<Map<String, String>> rlist = new ArrayList();
		Set<String> sets = new TreeSet<String>();
		sets.addAll(keys);
		for (String t : sets) {
			Map<String, String> tm = new HashMap();
			tm.put("id", t.replace("raceList:", ""));
			rlist.add(tm);
		}
		System.out.println(rlist);
	}

	// public static List<Map<String, String>> getHistory(String typeid) {
	// Set<String> keys = JedisUtil.keysByPre(typeid + ":");
	// List<Map<String, String>> rlist = new ArrayList();
	// Set<String> sets = new TreeSet<String>();
	// sets.addAll(keys);
	// for (String t : sets) {
	// Map<String, String> tm = new HashMap();
	// tm.put("id", t.replace(typeid + ":", ""));
	// rlist.add(tm);
	// }
	// return rlist;
	// }

	// public static void backup() {
	// Calendar myTime = Calendar.getInstance();
	// myTime.add(Calendar.DATE, -1);
	// Date beforeM = myTime.getTime();
	// String nday = new SimpleDateFormat("yyyyMMdd").format(beforeM);
	//
	// JedisUtil.set("raceList:" + nday, JedisUtil.get("raceList"));
	// JedisUtil.expire("raceList:" + nday, 10*24*60*60);
	//
	// JedisUtil.set("raceListDc:" + nday, JedisUtil.get("raceListDc"));
	// JedisUtil.expire("raceListDc:" + nday, 10*24*60*60);
	//
	// JedisUtil.set("raceListZc:" + nday, JedisUtil.get("raceListZc"));
	// JedisUtil.expire("raceListZc:" + nday, 10*24*60*60);
	// }

	public static void main(String[] args) {
		for (int i = 0; i < 75; i++) {
			Calendar myTime = Calendar.getInstance();
			myTime.add(Calendar.DATE, -i);
			Date beforeM = myTime.getTime();
			String nday = new SimpleDateFormat("yyyy-MM-dd").format(beforeM);
			System.out.println(nday);
			String url = "https://live.500.com?e=" + nday;
			if (!JedisUtil.exists("raceList:" + nday)) {
				Spider.create(new ServiceJc()).addUrl(url).run();
			}
		}
		//
		for (int i = 0; i < 75; i++) {
			Calendar myTime = Calendar.getInstance();
			myTime.add(Calendar.DATE, -i);
			Date beforeM = myTime.getTime();
			String nday = new SimpleDateFormat("yyyy-MM-dd").format(beforeM);
			System.out.println(nday);
			if (JedisUtil.exists("raceList:" + nday)) {
				List<RaceEntity> resultList = JSON.parseArray(JedisUtil.get("raceList:" + nday), RaceEntity.class);
				if (resultList != null && resultList.size() > 0) {
					ServiceTrend.processList(resultList);
					ServiceTouzhu.processList(resultList);
				}
			}
		}
	}
}
